package com.sbl.bajw.pc.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.sbl.bajw.common.MyBatisDao;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.common.web.SessionUnit;
import com.sbl.bajw.mapper.FeedBackDao;
import com.sbl.bajw.pc.model.AttFileModel;
import com.sbl.bajw.pc.model.FeedBackModel;
import com.sbl.bajw.util.ExecptionUtil;
import com.sbl.bajw.util.LogUtil;

@Service
public class FeedBackService {
	
	@Resource
	private FeedBackDao dao;
	
	@Resource
	private MyBatisDao gridDao;
	
	@Resource
	private AttFileService attfileService;
	
	static Logger log;
	
	static {
		log = LogUtil.getLogger("Car");
	}
	
	public void insert(FeedBackModel model) throws Exception{
		log.info(model.getCreater() + "开始入库，属性" + model.toString());
		try {
			uploadFile(model);
			dao.insert(model);
			log.info(model.getCreater() + "数据入库成功！");
		} catch (Exception e) {
			String msg = ExecptionUtil.getStackTrace(e);
			log.info("录入异常："+msg);
			throw e;
		}
	}
	
	public String getList(HashMap<String, Object> parm){
		String json = gridDao.selectPage(FeedBackDao.class, parm, "getList");
		return json;
	}
	
	public List<FeedBackModel> getListApp(String id){
		List<FeedBackModel> list = dao.getListApp(id);
		return list;
	}
	
	public HashMap<String, Object> getModelApp(String id){
		HashMap<String, Object> map = dao.getModelApp(id);
		return map;
	}
	
	public HashMap<String, Object> getModel(String id){
		HashMap<String, Object> map = dao.getModel(id);
		return map;
	}
	
	public List<HashMap<String, String>> getPics(String id){
		List<HashMap<String, String>> list = dao.getPics(id);
		return list;
	}
	
	public String uploadFile(FeedBackModel model) throws Exception {
		String id = dao.getFeedBackId();

		if (id == null || id.equals("")) {
			throw new MessageException("ID不能为空", "2");
		}

		String resultPath = "";
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssS");
		
		FileItem item = null;
		int i = 1;
		
		while((item = RequestUnit.getFileItem("img" + i)) != null){
			String dataStr = sf.format(Calendar.getInstance().getTime());
			
			AttFileModel filemodel = new AttFileModel();
			filemodel.setBusinessType("8");
			filemodel.setFileDir("feedback/image");
			filemodel.setFileName(dataStr + i +  "_img_" + id);
			filemodel.setGroupid(id);
			filemodel.setItem(item);
			filemodel.setUserid(model.getCreater());
			filemodel.setFiletype("img");
			attfileService.insert(filemodel);
			log.info(model.getCreater() + "图片上传成功：" + resultPath);
			i++;
		}
		
		FileItem record = RequestUnit.getFileItem("voice");
		if (record != null) {
			String dataStr = sf.format(Calendar.getInstance().getTime());
			
			AttFileModel filemodel = new AttFileModel();
			filemodel.setBusinessType("8");
			filemodel.setFileDir("feedback/voice");
			filemodel.setFileName(dataStr + "_record_" + id);
			filemodel.setGroupid(id);
			filemodel.setItem(record);
			filemodel.setUserid(model.getCreater());
			filemodel.setFiletype("record");
			attfileService.insert(filemodel);
			log.info(model.getCreater() + "录音上传成功:" + resultPath);
		} else {
			log.info(SessionUnit.getSessionId() + "没有录音数据");
		}
		
		model.setFeelbackId(id);
		
		return resultPath;
	}
}
